**********************************************
**** 		Figure 3 - Thoughts while Working
**********************************************


use "$dir/Data/analysis_main.dta", clear


reghdfe thinktask cash loan_moneylender numloans_worry bs_labor_30_nm bs_labor_30_miss i.future_fin_worry_nm if pid1==1 & lateprime==0 , vce(robust) a(roundgroup)
summ thinktask if e(sample) & cash==0

reghdfe thinkonlytask cash loan_moneylender numloans_worry bs_labor_30_nm bs_labor_30_miss i.future_fin_worry_nm if pid1==1 & lateprime==0 , vce(robust) a(roundgroup)
summ thinkonlytask if e(sample) & cash==0


***** Making the bar figure

gen int order = 1 if _n==1
replace order = 2 if _n==2
replace order = 3.5 if _n==4
replace order = 4.5 if _n==5

gen int order2 = 1 if _n==1
replace order2 = 2 if _n==2
replace order2 = 4 if _n==4
replace order2 = 5 if _n==5

gen int firstbars = .
replace firstbars = 1 if _n==1 | _n==4
replace firstbars = 0 if _n==2 | _n==5

gen mean1=.
gen mean2=.
gen mean3=.
gen mean4=.
gen pval=.
gen uconf_mean1 = .
gen lconf_mean1 = .



reghdfe thinktask cash loan_moneylender numloans_worry bs_labor_30_nm bs_labor_30_miss i.future_fin_worry_nm if pid1==1 & lateprime==0 , vce(robust) a(roundgroup)
local coef=_b[cash]
local secash=_se[cash]
test cash
replace pval = r(p) if _n==2

ci thinktask if e(sample)==1 & cash==0, level(90)
replace uconf_mean1 = r(ub) if _n==1
replace lconf_mean1 = r(lb) if _n==1

replace mean1 = r(mean) if _n==1
replace mean1 = r(mean) + `coef' if _n==2
replace uconf_mean1 = r(mean) + `coef' + (1.645*`secash') if _n==2
replace lconf_mean1 = r(mean) + `coef' - (1.645*`secash') if _n==2


replace mean2 = r(mean) if _n==1
replace mean2 = `coef' if _n==2
replace mean3 = r(mean) if _n==1 | _n==2
replace mean4 = 0 if _n==1
replace mean4 = `coef' if _n==2


reghdfe thinkonlytask cash loan_moneylender numloans_worry bs_labor_30_nm bs_labor_30_miss i.future_fin_worry_nm if pid1==1 & lateprime==0 , vce(robust) a(roundgroup)
local coef=_b[cash]
local secash=_se[cash]
test cash
replace pval = r(p) if _n==5
ci thinkonlytask if e(sample)==1 & cash==0, level(90)
replace uconf_mean1 = r(ub) if _n==4
replace lconf_mean1 = r(lb) if _n==4

replace mean1 = r(mean) if _n==4
replace mean1 = r(mean) + `coef' if _n==5
replace uconf_mean1 = r(mean) + `coef' + (1.645*`secash') if _n==5
replace lconf_mean1 = r(mean) + `coef' - (1.645*`secash') if _n==5

replace mean2 = r(mean) if _n==4
replace mean2 = `coef' if _n==5

replace mean3 = r(mean) if _n==4 | _n==5
replace mean4 = 0 if _n==4
replace mean4 = `coef' if _n==5

foreach x of varlist mean1 uconf_mean1 lconf_mean1 mean3{
	replace `x' = `x'*100
}

gen ucof_label = ""
replace ucof_label = "+ 0.115**"  if _n==2
replace ucof_label = "+ 0.137**" if _n==5

gen ucof_label2 = ""
replace ucof_label2 = "[{it:p} = 0.032]"  if _n==2
replace ucof_label2 = "[{it:p} = 0.044]" if _n==5


#delimit ;
twoway 	(bar mean1 order if firstbars==1, color("255 127 80") lwidth(medthick) fintensity(inten60) barwidth(.7))
		(bar mean1 order if firstbars==0, color("100 149 237") fintensity(inten100) barwidth(.7))
		(bar mean3 order if firstbars==0, color("white") barwidth(1))
		(scatter uconf_mean1 order, mlabel(ucof_label) msymbol(none) mlabposition(12)  mlabsize(medium) mlabcolor(black) mlabgap(7))
		(scatter uconf_mean1 order, mlabel(ucof_label2) msymbol(none) mlabposition(12)  mlabsize(medium) mlabcolor(black) mlabgap(2))	
		(rcap uconf_mean1 lconf_mean1 order, color(black)),
		yscale(range(110)) ylabel(0(20)100, nogrid) ytitle("Share of respondents (%)", size(medium))
		xscale(range(0.5 5)) xtitle("") xlabel(1.5 "Focused on work task" 4 `""Did not think about financial worries" "(only work task)""', noticks nogrid labsize(medium) labgap(2)) 
	   graphregion(color(white)) title("")
	   legend(lab(1 "Control mean") lab(2 "Treatment effect") size(medium) order(1 2) col(2) pos(6) region(lcolor(white)));
#delimit cr

graph export "$om/$f/Figure_3.eps", replace
graph export "$om/$f/Figure_3.png", replace
